package clinic.ms.service;


import clinic.ms.entity.*;
import clinic.ms.mapper.ItemMapper;
import clinic.ms.mapper.LogMapper;
import clinic.ms.mapper.LogdMapper;
import clinic.ms.mapper.PatientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class receptionService {
    @Autowired
    LogMapper logMapper;
    @Autowired
    PatientMapper patientMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    LogdMapper logdMapper;

    public List<Log> LogList() {
       List<Log> list= logMapper.selectAll();
       return list;
    }

    public Patient getpname(String patientNo) {
        //System.out.println(patientNo+"22");
        Patient pname = patientMapper.selectAllbyId(patientNo);
        return pname;
    }

    public List<Log> LogListop(String patientNo) {
            List<Log> list= logMapper.selectbyId(patientNo);
            return list;
    }

    public List<Patient> getbyname(String patientName) {
        List<Patient> pname = patientMapper.selectAllbyName(patientName);
        return pname;
    }

    public int insertLog(Log log) {
        int x=logMapper.insertLog(log);
        return x;
    }

    public Item getitembyId(String itemNo) {
        Item item= itemMapper.getitembyId(itemNo);
        return item;

    }

    public void updateItem(String itemNo,Integer logDNum) {
         itemMapper.updateItembyN(itemNo,logDNum);

    }

    public void insertLogd(LogD logd) {
        logdMapper.insertLogd(logd);
    }

    public Log getLogMax() {
        Log log=logMapper.getLogMax();
        return log;
    }

    public List<LogD> LogdList(Integer logNo) {
        List<LogD> list = logdMapper.LogdList(logNo);
        return list;
    }



    public void updateItemadd(String itemNo, Integer oldlogDNum) {
        itemMapper.updateItemaddbyN(itemNo,oldlogDNum);
    }


    public void updateLogd(String itemNo, Integer logDNum, Integer logDNo) {
        logdMapper.updateLogd(itemNo,logDNum,logDNo);

    }

    public void deleteLogD(Integer logDNo) {
        logdMapper.deleteLogD(logDNo);
    }

    public void deleteLog(Integer logNo) {
        logMapper.deleteLog(logNo);
    }

    public List<Log> LogListwei() {
        List<Log> list=logMapper.selectwei();
        return list;
    }

    public void updateLog(Integer logNo,String logStatus) {
        Date date=  new Date();
        SimpleDateFormat sd= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sd.format(date);
        System.out.println(now);
        logMapper.updateLog(logNo,logStatus,now);
    }

    public List<Log> LogListar() {
        List<Log> list=logMapper.selectar();
        return list;
    }

//    public void updateLogdf(Integer logNo) {
//        logdMapper.updateLogdf();
//    }

    //--
    public List<Pie> queryReceived(String begin,String end){
        List<Pie> pieList = new ArrayList<>();
        List<String> timeList = getMonthBetween(begin,end);

        for(String time:timeList){
            System.out.println(time);
            pieList.add(new Pie(time,0,logMapper.queryReceived(time)));
        }
        return pieList;

    }

    private List<String> getMonthBetween(String minDate, String maxDate){
        ArrayList<String> result = new ArrayList<String>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }
}
